﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using ZK.Common;
using ZK.HardWareSDK;
using ZK.InterfaceServer;

namespace ZK.HardWareBLL
{



    public class Onbon6K1CardBLL
    {
        public Onbon6K1CardSDK.bx_5k_area_header bx6k1;
        uint m_dwCurHand;
        public byte[] Text;
        public Common.HaredWareCommonM HaredWareCommonM = new Common.HaredWareCommonM();

        public Onbon6K1CardBLL(ZK.Common.HaredWareCommonM haredWareCommonM)
        {
            HaredWareCommonM = haredWareCommonM;
            GetHeader();
            //CreateClient();
        }



        public Onbon6K1CardBLL()
        {
            HaredWareCommonM = Common.IConfig.GetSetLedQ;

            Onbon6K1CardSDK.InitSdk(2, 2);
            GetHeader();
            //CreateClient();
        }
        /// <summary>
        /// 获取头部文件
        /// </summary>
        public void GetHeader()
        {
            // Onbon6K1CardSDK.bx_5k_area_header bx6k1 = new Onbon6K1CardSDK.bx_5k_area_header();
            // bx6k1.DynamicAreaLoc = Convert.ToByte(numericUpDown1.Value);
            bx6k1.AreaType = 0x06;
            bx6k1.AreaX = 0;
            bx6k1.AreaY = 0;
            bx6k1.AreaWidth = 96;
            bx6k1.AreaHeight = 48;

            bx6k1.Lines_sizes = 0;//行间距

            //byte[] RunMode_list = new byte[3];
            //RunMode_list[0] = 0;
            //RunMode_list[1] = 1;
            //RunMode_list[2] = 2;
            //int rl = comboBox3.SelectedIndex;
            bx6k1.RunMode = 0;
            //bx6k1.RunMode = Convert.ToByte(comboBox3.SelectedIndex+1);

            bx6k1.Timeout = Convert.ToInt16(2);


            bx6k1.Reserved1 = 0;
            bx6k1.Reserved2 = 0;
            bx6k1.Reserved3 = 0;
            //byte[] SingleLine_list = new byte[2];
            //SingleLine_list[0] = 0x01;
            //SingleLine_list[1] = 0x02;
            //int sll = comboBox1.SelectedIndex;
            bx6k1.SingleLine = 0x02;
            //bx6k1.SingleLine = Convert.ToByte(comboBox1.SelectedIndex);

            //byte[] NewLine_list = new byte[2];
            //NewLine_list[0] = 0x01;
            //NewLine_list[1] = 0x02;
            //int nl = comboBox2.SelectedIndex;
            bx6k1.NewLine = 0x02;
            //bx6k1.NewLine = Convert.ToByte(comboBox2.SelectedIndex);


            //byte[] DisplayMode_list = new byte[6];
            //DisplayMode_list[0] = 0x01;
            //DisplayMode_list[1] = 0x02;
            //DisplayMode_list[2] = 0x03;
            //DisplayMode_list[3] = 0x04;
            //DisplayMode_list[4] = 0x05;
            //DisplayMode_list[5] = 0x06;
            //int dml = comboBox4.SelectedIndex;
            bx6k1.DisplayMode = 0x01;
            //bx6k1.DisplayMode = Convert.ToByte(comboBox4.SelectedIndex);

            bx6k1.ExitMode = 0x00;


            bx6k1.Speed = (byte)0;
            //bx6k1.Speed=Convert.ToByte(comboBox5.SelectedIndex);

            bx6k1.StayTime = Convert.ToByte(10);
            //AreaTxt = "wohenhao ";
            // return bx6k1;

        }
        /// <summary>
        /// 初始化客户端
        /// </summary>
        public void CreateClient()
        {
            byte[] led_ip = System.Text.Encoding.ASCII.GetBytes(HaredWareCommonM.Ip);
            uint led_port = Convert.ToUInt32("5005");
            uint hand = Onbon6K1CardSDK.CreateClient(led_ip, led_port, Onbon6K1CardSDK.bx_5k_card_type.BX_6K1, 1, 1, null);
            m_dwCurHand = hand;
            if (hand == 0)
            {
                string res = "连接控制器失败 ip:" + HaredWareCommonM.Ip + " port:5005";
                AutoControlTransportApiServer.AddHardWareLog(HaredWareCommonM.Name, 1, res);
                Common.Log4.LogManage.WriteErr(res);

            }
            else
            {
                // MessageBox.Show("连接控制器成功");
            }

        }


        /// <summary>
        /// 动态区域更新函数
        /// </summary>
        /// <param name="text"></param>
        public void SCREEN_SendDynamicArea(string text)
        {
            // var onbon6K1 = GetHeader();
            // if (i == 0 || i == 1)
            // {
            //SoundArea a = new SoundArea(type);
            // DialogResult ret = a.ShowDialog();

            //if (ret == DialogResult.OK)
            //{
            CreateClient();

            int x;
            //if (a.SoundMode == 0)
            //{
            Text = System.Text.Encoding.Default.GetBytes(text);
            bx6k1.DataLen = Text.Length;
            x = Onbon6K1CardSDK.SCREEN_SendDynamicArea(m_dwCurHand, bx6k1, (ushort)bx6k1.DataLen, Text);
            //}
            //else
            //{
            //    x = Onbon6K1CardSDK.SCREEN_SendSoundDynamicArea(m_dwCurHand, a.bx6k1, (ushort)a.bx6k1.DataLen, SoundArea.AreaText, a.SoundMode, a.SoundPerson, a.SoundVolume, a.SoundSpeed, a.SoundDataLen, SoundArea.SoundAreaText);
            //}

            if (x == 0)
            {
                //MessageBox.Show("动态区域更新成功");
            }
            else
            {
                // MessageBox.Show("动态区域更新失败");
                Common.Log4.LogManage.WriteErr("动态区域更新失败 ip:" + HaredWareCommonM.Ip + " port:" + HaredWareCommonM.Port + " err code:" + x);
            }
            Destroy(m_dwCurHand);
        }

        public void Destroy(uint m_dwCurHand)
        {
            Onbon6K1CardSDK.Destroy(m_dwCurHand);
        }

        public void ReleaseSdk()
        {
            Onbon6K1CardSDK.ReleaseSdk();
        }

        ~Onbon6K1CardBLL()
        {
            ReleaseSdk();
        }
    }

}
